package com.mbc.web.common.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.common.entities.Country;
import com.mbc.common.entities.CountryRpt;
import com.mbc.web.common.client.ClientSingleton;
import com.mbc.web.common.client.CommonControllerMgr;
import com.mbc.web.common.client.CommonMessage;
import com.mbc.web.common.client.ViewUtils;
import com.mbc.web.common.client.ui.Modal;
import com.mbc.web.common.client.ui.control.MButton;
import com.mbc.web.common.client.ui.control.MTextBox;
import com.mbc.web.common.shared.HttpStatus;

public class FrmCountryRpt extends Composite {

	@UiField
	MButton btnPreview;
	@UiField
	MButton btnClear;
	@UiField MButton btnBack;
	@UiField
	MButton btnModalFromCountryID;
	@UiField
	MButton btnModalToCountryID;
	@UiField
	MTextBox txtfromCountryID;
	@UiField
	MTextBox txttoCountryID;

	private Country country;

	private static FrmCountryRptUiBinder uiBinder = GWT
			.create(FrmCountryRptUiBinder.class);

	interface FrmCountryRptUiBinder extends UiBinder<Widget, FrmCountryRpt> {
	}

	public FrmCountryRpt() {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
	}

	public FrmCountryRpt(CountryRpt country) {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
	}

	private void initialize() {
		
		txtfromCountryID.getElement().setAttribute("style", "text-transform: uppercase;");
		txttoCountryID.getElement().setAttribute("style", "text-transform: uppercase;");
		
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {

			@Override
			public void execute() {
				txtfromCountryID.setFocus(true);
			}
		});
		txttoCountryID.addKeyDownHandler(new KeyDownHandler() {
			
			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					event.preventDefault();
					TextBox l_txt = (TextBox) event.getSource();
					if (l_txt == txttoCountryID)
						btnPreview.setFocus(true);
				}
			}
		});
	}

	private CountryRpt bindInfoCountry() {
		CountryRpt l_info = new CountryRpt();
		l_info.setFromCountryID(txtfromCountryID.getText());
		l_info.setToCountryID(txttoCountryID.getText());
		return l_info;

	}

	@UiHandler("btnPreview")
	void onBtnPreviewClick(ClickEvent event) {
		ViewUtils.closeMessageBox();
		ViewUtils.startAjaxLoader(CommonMessage.loading_message);
		CommonControllerMgr.prepareCountryReport(bindInfoCountry(),
				CountryReportCallback());
	}

	private AsyncCallback<Integer> CountryReportCallback() {
		return new AsyncCallback<Integer>() {

			@Override
			public void onSuccess(Integer result) {
				ViewUtils.stopAjaxLoader();
				if (result == HttpStatus.OK) {
					ClientSingleton.viewReport("Country Report");
				} else if (result == HttpStatus.NOT_FOUND) {
					ViewUtils.showErrorMessage("No records.");
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}

	@UiHandler("btnBack")
	void onBtnBackClick(ClickEvent event) {
		ClientSingleton.changeView(new FrmCountry());
	}
	
	@UiHandler("btnClear")
	void onBtnClearClick(ClickEvent event) {
		txtfromCountryID.setText("");
		txttoCountryID.setText("");
		txtfromCountryID.setFocus(true);
	}

	@UiHandler("btnModalFromCountryID")
	void onBtnBwrfromCountryClick(ClickEvent event) {
		final SearchCountry l_dia = new SearchCountry("Country List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					country = l_dia.getSelectedEntity();
					txtfromCountryID.setText(country.getCountryCode());

					txttoCountryID.setFocus(true);
				} else
					txtfromCountryID.setFocus(true);
			}
		});
	}

	@UiHandler("btnModalToCountryID")
	void onBtnBwrtoCountryClick(ClickEvent event) {
		final SearchCountry l_dia = new SearchCountry("Country List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					country = l_dia.getSelectedEntity();
					txttoCountryID.setText(country.getCountryCode());
					txttoCountryID.setFocus(true);
				} else
					txttoCountryID.setFocus(true);
			}

		});
	}
	@UiHandler("txtfromCountryID")
	void onTxtFromCountryChange(ChangeEvent event) {
		if (!txtfromCountryID.getText().equals("")){
			Country l_info = new Country();
			l_info.setCountryCode(txtfromCountryID.getText());
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findCountryByCode(l_info,
					getFromCountryCallback());
		}
	}

	private AsyncCallback<Country> getFromCountryCallback() {
		return new AsyncCallback<Country>() {

			@Override
			public void onSuccess(Country result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txtfromCountryID.setText(result.getCountryCode());
					txtfromCountryID.getElement().setAttribute("id",
							String.valueOf(result.getCountryId()));
				} else {
					txtfromCountryID.setText("");
					txtfromCountryID.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Country Code does not exist.");
					txtfromCountryID.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	@UiHandler("txttoCountryID")
	void onTxtToCountryChange(ChangeEvent event) {
		if (!txttoCountryID.getText().equals("")){
			Country l_info = new Country();
			l_info.setCountryCode(txttoCountryID.getText());
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findCountryByCode(l_info,
					getToCountryCallback());
		}
	}

	private AsyncCallback<Country> getToCountryCallback() {
		return new AsyncCallback<Country>() {

			@Override
			public void onSuccess(Country result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txttoCountryID.setText(result.getCountryCode());
					txttoCountryID.getElement().setAttribute("id",
							String.valueOf(result.getCountryId()));
				} else {
					txttoCountryID.setText("");
					txttoCountryID.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Country Code does not exist.");
					txttoCountryID.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	
	@UiHandler("txtfromCountryID")
	void onTxtfromCountryKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onBtnBwrfromCountryClick(null);
		}
	}
	@UiHandler("txttoCountryID")
	void onTxttoCountryKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onBtnBwrtoCountryClick(null);
		}
	}
}
